package cn.broadmax.boot.modules.sys.controller;


import cn.broadmax.boot.base.SuperController;
import cn.broadmax.boot.log.SysLog;
import cn.broadmax.boot.modules.sys.entity.SysDictItem;
import cn.broadmax.boot.base.message.SResult;
import cn.broadmax.boot.modules.sys.bean.dict.DictSearch;
import cn.broadmax.boot.modules.sys.entity.SysDict;
import cn.broadmax.boot.modules.sys.service.ISysDictItemService;
import cn.broadmax.boot.modules.sys.service.ISysDictService;
import cn.broadmax.boot.util.SAssert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jexx.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 字典 前端控制器
 * </p>
 *
 * @author jexx
 * @since 2021-05-19
 */
@Api(tags = "字典")
@RestController
@RequestMapping("/sys/dict")
@RequiredArgsConstructor
public class SysDictController extends SuperController {

    private final ISysDictService sysDictService;
    private final ISysDictItemService sysDictItemService;

    @ApiOperation("新增字典")
    @SysLog(value = "新增字典")
    @PostMapping(value = "")
    public SResult<SysDict> addDict(@RequestBody SysDict dict){
        dict = sysDictService.addDict(dict);
        return SResult.ok("", dict);
    }

    @ApiOperation("根据主键删除字典")
    @SysLog(value = "删除字典")
    @DeleteMapping(value = "/{dictId}")
    public SResult<Boolean> deleteDictById(@PathVariable String dictId){
        boolean success = sysDictService.removeById(dictId);
        return SResult.ok("", success);
    }

    @ApiOperation("根据主键更新字典信息")
    @SysLog(value = "更新字典")
    @PutMapping(value = "/{dictId}")
    public SResult<SysDict> updateDictById(@PathVariable String dictId, @RequestBody SysDict dict){
        SAssert.notNull(dict, "dict must be not null");
        SysDict dict1 = sysDictService.updateDictById(dictId, dict);
        return SResult.ok("", dict1);
    }

    @ApiOperation("分页查询字典")
    @GetMapping(value = "")
    public SResult<IPage<SysDict>> pageDict(DictSearch search){
        LambdaQueryWrapper<SysDict> wrapper = DictSearch.createWrap(search);
        IPage<SysDict> page = sysDictService.page(getPage(), wrapper);
        return SResult.ok("", page);
    }

    @ApiOperation("新增字典项")
    @SysLog(value = "新增字典项")
    @PostMapping(value = "/{dictId}/item")
    public SResult<SysDictItem> addDictItem(String dictId, @RequestBody SysDictItem dictItem){
        SysDictItem item = sysDictItemService.addDictItem(dictId, dictItem);
        return SResult.ok("", item);
    }

    @ApiOperation("根据主键删除字典项")
    @SysLog(value = "删除字典项")
    @DeleteMapping(value = "/item/{dictItemId}")
    public SResult<Boolean> deleteUserById(@PathVariable String dictItemId){
        boolean success = sysDictItemService.removeById(dictItemId);
        return SResult.ok("", success);
    }

    @ApiOperation("根据主键更新字典项")
    @SysLog(value = "更新字典项")
    @PutMapping(value = "/item/{dictItemId}")
    public SResult<SysDictItem> updateUserById(@PathVariable String dictItemId, @RequestBody SysDictItem dictItem){
        SAssert.notNull(dictItem, "dictItem must be not null");
        SysDictItem item = sysDictItemService.updateDictItemById(dictItemId, dictItem);
        return SResult.ok("", item);
    }

    @ApiOperation("根据字典ID获取字典项列表")
    @GetMapping(value = "/{dictId}/item/list")
    public SResult<List<SysDictItem>> pageUsers(@PathVariable String dictId){
        if(StringUtil.isEmpty(dictId)){
            return SResult.ok("", null);
        }
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictItem::getDictId, dictId);
        List<SysDictItem> list = sysDictItemService.list(wrapper);
        return SResult.ok("", list);
    }

}
